package de.koller.kelvin.engine.landscape;

import de.koller.aframe.plain.Sprite;
import de.koller.aframe.util.misc.BufferedRandomFloat;
import de.koller.aframe.util.misc.ColorGradient;
import de.koller.aframe.util.noise.n2D.Perlin2D;
import de.koller.aframe.util.noise.n2D.ScrollingPerlin2D;
import de.koller.kelvin.KRoot;
import de.koller.kelvin.engine.TileMap;

public class SurfaceGenerator {
	
	private static final float MAX_HEIGHT = Perlin2D.maxHeight( 0, 7, .7f );
	private final BufferedRandomFloat brf = new BufferedRandomFloat( "./temp/kelvin/surface", KRoot.RND );
	private final ScrollingPerlin2D perlin = new ScrollingPerlin2D( brf, 7, .7f );
	
	public void setup( LandscapeTile tile ) {
		Sprite s = new Sprite( TileMap.TILE_SIZE, TileMap.TILE_SIZE );
		
		for( int x = 0; x < TileMap.TILE_SIZE; x++ )
			for( int y = 0; y < TileMap.TILE_SIZE; y++ ) {
				float f = perlin.getValue(  .1f / TileMap.TILE_SIZE * x + tile.tx*.1f, 
											.1f / TileMap.TILE_SIZE * y + tile.ty*.1f ); 
				s.raster.setPixel( ColorGradient.cubic( 1f / MAX_HEIGHT * f, 0xff000000, 0xffffffff ), x, y );
			}
		
		tile.surfaceView = s;
	}
	
}
